/**
 * @name 公文类型的类js
 *
 * @type {{submit, index}}
 */
var AppsType= function () {

    /**
     *
     * @type {{super: {success: boolean, message: string}, position: {success: boolean, message: string}, department: {success: boolean, message: string}, user: {success: boolean, message: string}}}
     */
    var errors={
        'super':{
            success:false,
            message:"请选择监控人员"
        },
        'position':{
            success:false,
            message:"请上选择岗位"
        },
        'department':{
            success:false,
            message:"请选择部门"
        },
        'user':{
            success:false,
            message:"请选择人员"
        }
    }

    var supers= [];

    var users=[];

    var position=[];

    var selectHandler=function () {
        $('input[name=use]').on('ifChecked',function () {
            var div= $(this).closest('.i-checks');
            var index= div.index();
            console.log(index);
            div.parent().nextAll('.input-group').addClass('hidden');
            div.parent().nextAll('.input-group:eq('+index+')').removeClass('hidden');
        });
        $('.ibox').on('click','button.department',function () {
            parent.layer.open({
                type:2,
                shade:0.8,
                shadeClass:false,
                area:['900px','600px'],
                btn:['确认'],
                yes:function(index,layers){
                    var iframe= layers.find('iframe');
                    var checkboxs= iframe.contents().find('input[type=checkbox]:checked');
                    if (checkboxs==0){
                        parent.layer.close(index);
                        Common.alert(errors.department);
                        return;
                    }
                    var str= '';
                    var ids='';
                    checkboxs.each(function () {
                        var name= $(this).data('name');
                        var id= $(this).data('id');
                        if (str==''){
                            str= name;
                        } else{
                            str+=","+name;

                        }
                        $('#departmentName').val(str);
                        $('body').append('<input type="hidden" name="departmentId" value="'+id+'"/>');
                        parent.layer.close(index);
                    })
                },
                content:'../department/index.html?check=checkbox'
            })
        });
        $('.ibox').on('click','button.position',function () {
            parent.layer.open({
                type:2,
                shade:0.8,
                shadeClass:false,
                area:['900px','600px'],
                btn:['确认'],
                yes:function(index,layers){
                    var iframe= layers.find('iframe');
                    var checkboxs= iframe.contents().find('input[type=checkbox]:checked');
                    if (checkboxs==0){
                        parent.layer.close(index);
                        Common.alert(errors.department);
                        return;
                    }
                    var str= '';
                    var ids='';
                    checkboxs.each(function () {
                        var name= $(this).data('name');
                        var id= $(this).data('id');
                        if (str==''){
                            str= name;
                        } else{
                            str+=","+name;

                        }
                        $('#positionName').val(str);
                        $('body').append('<input type="hidden" name="positionId" value="'+id+'"/>');
                        parent.layer.close(index);
                    })
                },
                content:'../position/index.html?check=checkbox'
            })
        });

        $('.ibox').on('click','button.super',function () {
            parent.layer.open({
                type:2,
                shade:0.8,
                shadeClass:false,
                area:['900px','600px'],
                btn:['确认'],
                yes:function(index,layers){
                    var iframe= layers.find('iframe');
                    var checkboxs= iframe.contents().find('input[type=checkbox]:checked');
                    if (checkboxs==0){
                        parent.layer.close(index);
                        Common.alert(errors.department);
                        return;
                    }
                    var str= '';
                    var ids='';
                    checkboxs.each(function () {
                        var name= $(this).data('name');
                        var id= $(this).data('id');
                        if (str==''){
                            str= name;
                        } else{
                            str+=","+name;
                        }
                        $('body').append('<input type="text" name="superId" value="'+id+'"/>');
                    })
                    $('#superName').val(str);
                    parent.layer.close(index);
                },
                content:'../user/index.html?check=checkbox'
            })
        });

        $('.ibox').on('click','button.user',function () {
            parent.layer.open({
                type:2,
                shade:0.8,
                shadeClass:false,
                area:['900px','600px'],
                btn:['确认'],
                yes:function(index,layers){
                    var iframe= layers.find('iframe');
                    var checkboxs= iframe.contents().find('input[type=checkbox]:checkbox');
                    if (checkboxs==0){
                        parent.layer.close(index);
                        Common.alert(errors.department);
                        return;
                    }
                    var str= '';
                    var ids='';
                    checkboxs.each(function () {
                        var name= $(this).data('name');
                        var id= $(this).data('id');
                        if (str==''){
                            str= name;
                        } else{
                            str+=","+name;
                        }
                        $('#userName').val(str);
                        $('body').append('<input type="hidden" name="userId" value="'+id+'"/>');
                        parent.layer.close(index);
                    })
                },
                content:'../user/index.html?check=checkbox'
            })
        })
    };

    /**
     * 校验表单的函数
     *
     */
    var validateHandler=function () {
        $('form').validate({
            rules:{
                title:{
                    required:true,
                    remote:{
                        url:"validate",
                        type:"get"
                    }
                }
            },
            messages:{
                title:{
                    required:"必填",
                    remote:"类型已经存在"
                }
            },
            submitHandler: function (form) {

                var superHiddens= $('input[name=superId]');
                console.log(superHiddens[0]);
                if (superHiddens.length==0){
                    Common.alert(errors.super);
                    return;
                }
                var fileType= $('input[name=type]:checked').val();
                var useType= $('input[name=use]:checked').val();
                var userHiddens= $('input[name=userId]');
                if (useType==0 && userHiddens.length==0){
                    Common.alert(errors.user);
                    return;
                }
                var departmentHiddens= $('input[name=departmentId]');
                if (useType==1 && departmentHiddens.length==0){
                    Common.alert(errors.department);
                    return;
                }
                var positionHiddens= $('input[name=positionId]');
                if (useType==2 && positionHiddens.length==0){
                    Common.alert(errors.position);
                    return;
                }
                var superIds= [];
                superHiddens.each(function () {
                    superIds.push($(this).val());
                });
                var userIds=[];
                userHiddens.each(function () {
                    userIds.push($(this).val());
                });
                var departmentIds= [];
                departmentHiddens.each(function () {
                    departmentIds.push($(this).val())
                });
                var positionIds= [];
                positionHiddens.each(function () {
                    positionIds.push($(this).val())
                })
                var params={};
                params.title= $('#title').val();
                params.context= $('#description').val();
                params.fileType= fileType;
                params.supervisions= superIds;
                if (useType==0){
                    params.users= userIds;
                }else if (useType==1){
                    params.departments= departmentIds;
                } else if (useType==2){
                    params.positions=positionIds;
                }
                params.useType= useType;
                params= JSON.stringify(params);
                $.ajax({
                    type:"post",
                    url:"submit",
                    data:{'params':params},
                    dataType:"json",
                    success:function (data) {
                        Common.alert(data);
                        if (data.success){
                            setTimeout(function () {
                                location.href="index.html";
                            },1500)
                        }
                    }
                })
            }
        })
    };

    var buttonHandler= function () {
        $('table').on("click",'a.btn',function () {
            if (!$(this).children().hasClass('fa-server')){
                return;
            }
            var id= $(this).data("id");
            parent.layer.open({
                type:2,
                shade:0.8,
                shadeClass:false,
                area:['1000px','750px'],
                btn:['确认'],
                yes:function(index,layers){
                    var iframe= layers.find('iframe');
                    var typeId= id;
                    var content= iframe[0].contentWindow.returnHtml();
                    var params={};
                    params.typeId= typeId;
                    params.content= content;
                    params= JSON.stringify(params);
                    console.log(params);
                    $.ajax({
                        type:"post",
                        url:"../tplhtml/submit",
                        data:{'params':params},
                        dataType:"json",
                        success:function (data) {
                            parent.layer.close(index);
                            Common.alert(data);
                        }
                    })
                },
                content:'../tplhtml/index.html?typeId='+id
            })
        })
    }

    return{
        submit:function () {
            selectHandler();
            validateHandler();
        },
        index:function () {
            buttonHandler();
        }
    }
}()